﻿
import re

class Config(object):
    pattern=r"(?<={{User:gqqnb/ArchiveConfig)(\s*\n*\|\w+\s*=\s*[^\n|]+)+\s*\n*(?=}})"

    def __init__(self, configText):
        matches = re.findall(r"\w+\s*=[^|]+",configText)
        self.__dict = {}
        for match in matches:
            kv = match.split('=')
            self.__dict[kv[0].strip()] = kv[1].strip()



    def __getitem__(self, key):
        if(key == 'archiveheader' and key not in self.__dict):
            return "{{talkarchive}}"

        if(key =='minthreadstoarchive' and key not in self.__dict):
            return 2

        return self.__dict[key]

    def __setitem__(self, key, value):
        self.__dict[key] = value
        
    def __contains__(self, item):
        return item in self.__dict
    
    @property
    def archiveByCounter(self):
        """获取是不是计数存档"""
        return self.__dict['archive'].index("{counter}") > -1

    @property
    def archiveByDate(self):
        """获取是不是日期存档"""
        return self.__dict['archive'].index("{year}") > -1 or \
                self.__dict['archive'].index("{month}") > -1 or \
                self.__dict['archive'].index("{day}") > -1

    def __str__(self):
        configText = ''
        for key in self.__dict:
            configText+="| " + key + " = " + str(self.__dict[key]) + "\r\n"
        return configText